package cn.jsp;

import cn.jsp.dao.BaseDao;
import cn.jsp.domain.Stu;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/rowProcessor")
public class RowProcessorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getServletContext());
        BaseDao dao=ctx.getBean(BaseDao.class);
        List<Stu> res=dao.query("select * from stu", rs -> {
            Stu stu= null;
            try {
                stu = new Stu(rs.getString("name"),rs.getInt("age"));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return stu;
        });
        resp.getWriter().println(res);
    }
}
